package com.st.shopadmin.service.impl;

import com.github.pagehelper.PageInfo;
import com.st.shopadmin.common.ServerResponse;
import com.st.shopadmin.dao.MessageContentMapper;
import com.st.shopadmin.dao.MessageTargetMapper;
import com.st.shopadmin.pojo.MessageContent;
import com.st.shopadmin.pojo.MessageTarget;
import com.st.shopadmin.pojo.Shop;
import com.st.shopadmin.pojo.TaskInfo;
import com.st.shopadmin.service.SystemNoticeService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SystemNoticeServiceImpl implements SystemNoticeService{
    @Autowired
    private MessageContentMapper messageContentMapper;
    @Autowired
    private MessageTargetMapper messageTargetMapper;

    @Override
    public ServerResponse<List> getLists(int pageNum, int pageSize) {
        List<MessageContent> messageContents = messageContentMapper.selectList();
        return  ServerResponse.createBySuccess(messageContents);
    }

    @Override
    public ServerResponse<PageInfo> page(int pageNum, int pageSize) {
        List<MessageContent> productList = messageContentMapper.selectList();
        PageInfo pageResult = new PageInfo(productList);
        return  ServerResponse.createBySuccess(pageResult);
    }

    @Override
    @Transactional
    public ServerResponse<String> addMessage(MessageContent messageContent) {
        messageContent.setFromUid(1L);//此处存放当前登录用户
        messageContent.setMesageType(100);//100表示站内信息
        int resultCount = messageContentMapper.insertSelective(messageContent);
        if(resultCount >0 ){
            MessageTarget messageTarget = new MessageTarget();
            messageTarget.setFromUid(1L);//此处存放当前登录用户
            messageTarget.setToUid(100L);//100表示发送给全部用户
            messageTarget.setMessageId(messageContent.getId());//信息Id
            messageTargetMapper.insertSelective(messageTarget);
            return ServerResponse.createBySuccess("保存成功");
        }else{
            return  ServerResponse.createByErrorMessage("保存失败");
        }
    }

    @Transactional
    public  ServerResponse updateMessage(String title ,String content,Long messageId){
        if(messageId == null || title == null){
            return   ServerResponse.createByErrorMessage("参数错误");
        }
        MessageContent messageContent = new MessageContent();
        messageContent.setId(messageId);
        messageContent.setTitle(title);
        messageContent.setContent(content);
        int rowCount  = messageContentMapper.updateByPrimaryKeySelective(messageContent);
        if(rowCount > 0){
            return  ServerResponse.createBySuccess("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    @Override
    public ServerResponse<MessageContent> detailMessage(Long messageId) {
        if(messageId == null){
            return  ServerResponse.createByErrorMessage("参数错误");
        }
        MessageContent messageContent = messageContentMapper.selectByPrimaryKey(messageId);
        if(messageContent == null){
            return  ServerResponse.createByErrorMessage("不存在");
        }
        return  ServerResponse.createBySuccess(messageContent);
    }


}
